package cn.i96688.scanclient.utils;

import java.io.File;

import com.alibaba.fastjson.JSONObject;
import cn.i96688.scanclient.bean.PayLog;
import cn.i96688.scanclient.exception.PayException;

/**
 * 本地存储
 * @author xys
 *
 */
public class LocalStore {
//	private Logger log=Logger.getLogger(LocalStore.class);
	
	private final static String merFile="mer.store";
	private final static String orderFile="order.store";
	private final static String pdSizeFile="pdsize.store";
	public final static String storeDir;
	
	private JSONObject params=new JSONObject();
	private JSONObject pdSizeJson=new JSONObject();
	
	static{
		storeDir=new File(System.getProperty("user.home")).getPath()+File.separator+".hyb"+File.separator;
	}
	
	private static LocalStore instance=new LocalStore();
	public static LocalStore getInstance(){
		return instance;
	}
	
	public JSONObject getOrderInf(){
		String str = readFile(storeDir+orderFile,"从本地读取订单信息出错");
		try{
			return JSONObject.parseObject(str);
		}catch(Throwable t){
			PayLog.getInstance().addLogs(t);
			return new JSONObject();
		}
	}
	
	public JSONObject getMerInf(){
		String str=readFile(storeDir+merFile,"从本地读取商户信息出错");
//		byte[] bytes=Base64.getDecoder().decode(str.getBytes());
//		if(bytes.length==0){
//			return new JSONObject();
//		}
		if(str==null || str.length()<=0)
			return new JSONObject();
		try{
			JSONObject merInf = JSONObject.parseObject(str);
			params.put("merInf", merInf);
			return merInf;
		}catch(Throwable t){
			t.printStackTrace();
			return new JSONObject();
		}
	}
	
	public void writeMerInf(JSONObject json){
		if(json==null){
			throw new PayException("传入的商户信息为空");
		}
		String wrStr=json.toString();
//		byte[] wrs=Base64.getEncoder().encode(wrStr.getBytes());
		if(writeFile(storeDir+merFile,wrStr.getBytes(),"写入商户信息到本地出错")){
			params.put("merInf", json);
		}
	}
	public void deleteMerInf(){
		File file=new File(storeDir+merFile);
		file.deleteOnExit();
	}
	public void writeOrderInf(JSONObject json){
		if(json==null){
			System.err.println("写入订单信息到本地出错");
		}else{
			try {
				writeFile(storeDir+orderFile,json.toString().getBytes("UTF_8"),"写入订单信息到本地出错");
			} catch (Exception e) {
				PayLog.getInstance().addLog("写入订单信息到本地出错");
				PayLog.getInstance().addLogs(e);
			}
		}
	}
	
	public JSONObject getPayDialogConfig(){
		String str=readFile(storeDir+pdSizeFile,"从本地读取支付框配置信息出错");
		if(str==null || str.length()<=0)
			return new JSONObject();
		try{
			pdSizeJson= JSONObject.parseObject(str);
			return pdSizeJson;
		}catch(Throwable t){
			t.printStackTrace();
			return new JSONObject();
		}
	}
	
	public void writePayDialogConfig(JSONObject confiJson){
		String wrStr=confiJson.toString();
		if(writeFile(storeDir+pdSizeFile,wrStr.getBytes(),"写入支付框配置信息到本地出错")){
			pdSizeJson=confiJson;
		}
	}
	
	private boolean writeFile(String path,byte[] content,String errTip){
		try {
			File thePath=new File(path);
			File dir=thePath.getParentFile();
			if(!dir.isDirectory()){
				dir.mkdirs();
			}
			if(!thePath.isFile()){
				thePath.createNewFile();
			}
			PayUtil.writeFile(path, new String(content));
		} catch (Exception e) {
			PayLog.getInstance().addLog(errTip);
			PayLog.getInstance().addLogs(e);
			return false;
		}
		return true;
	}
	
	private String readFile(String path,String errTip){
		try {
			File thePath=new File(path);
			if(!thePath.isFile())
				return "";
			return PayUtil.readFile(thePath.getPath());
		} catch (Exception e) {
			PayLog.getInstance().addLog(errTip);
			PayLog.getInstance().addLogs(e);
			return "";
		}
	}
	
}
